<script setup lang="ts">
import { ref, onMounted } from "vue";
import { getKnowledgeList, knowledgeEmbedding } from "@/api/lms/knowledge";
import { ElMessage } from "element-plus";
const dialogVisible = ref(false);

const showDialogVisible = () => {
  if (coursewareIds.value.length == 0) {
    ElMessage({
      message: "请先选择需要处理的文件",
      type: "warning"
    });
    return;
  }

  dialogVisible.value = true;
};

const setCoursewareIds = (ids: string[]) => {
  coursewareIds.value = ids;
};

const knowledgeList = ref();
const list = ref([]);
const knowledgeId = ref("");
const currentKnowledge = ref("embedding模型确认后不能修改，请注意!");

const coursewareIds = ref([]);

const selectChange = val => {
  console.log(val);
  knowledgeId.value = val;
  knowledgeList.value.forEach(item => {
    if (item.id == val) {
      console.log(item);
      knowledgeId.value = item.id;
      currentKnowledge.value =
        "embedding模型确认后不能修改，请注意! 当前模型：" + item.embeddingModel;
    }
  });
};

const onSubmit = () => {
  const data = {
    knowledgeId: knowledgeId.value,
    coursewareIds: coursewareIds.value
  };
  knowledgeEmbedding(data).then(_ => {
    dialogVisible.value = false;
  });
};

onMounted(() => {
  getKnowledgeList("").then(res => {
    knowledgeList.value = res;
    const li = [];
    res.forEach(item => {
      li.push({
        label: item.name,
        value: item.id
      });
      list.value = li;
    });
  });
});

defineExpose({
  showDialogVisible,
  setCoursewareIds
});
</script>

<template>
  <div>
    <el-dialog v-model="dialogVisible" title="请选择一个需要加入的知识库">
      <el-form ref="elForm" label-width="100px">
        <el-form-item label="知识库" prop="coursewareFolderId">
          <el-select
            v-model="knowledgeId"
            placeholder="请选择知识库"
            clearable
            @change="selectChange"
            :style="{ width: '100%' }"
          >
            <el-option
              v-for="(item, index) in list"
              :key="index"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <el-alert :title="currentKnowledge" type="warning" />
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="onSubmit"> 提交 </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<style scoped></style>
